#include "TableId.hpp"
#include <sstream>
#include <stdlib.h>

int TableId::add(const string identifiant)
{
	int num = getNum(identifiant);
	if(num == -1) {
		champ.insert(std::make_pair(champ.size(), identifiant));
		return champ.size() - 1;
	}
	return num;
}


string TableId::getId(const int num) const
{
	if(num == -1)
	{
		return "";
	}

	map<const int, const string>::const_iterator it;
	it = champ.find(num);

	if(it == champ.end()) {
		cerr << "ERROR : NO ID WITH NUM = " << num << endl;
		exit(22);
		//return NULL;
	}
	return it->second;
}

int TableId::getNum(const string &id) const
{
	map<const int, const string>::const_iterator it;
	for(it = champ.begin(); it != champ.end(); it++)
	{
		if(id.compare(it->second) == 0)
		{
			return it->first;
		}
	}
	return -1;
}

void TableId::print() const
{
	map<const int, const string>::const_iterator itr;
	for(itr = champ.begin(); itr != champ.end(); ++itr)
	{
	    cout << itr->second << " "
			<< itr->first << endl;
	}
}

const map<const int, const string> * TableId::getData()
{
	return &this->champ;
}
